package test3_17.Account;

public class CheckAccount extends Account{
    /*
    该账户中定义一个属性overdraft代表可透支限额
     */

    private double overdraft;

    public CheckAccount(String id, double balance, double annualInterestRate, double overdraft) {
        super(id, balance, annualInterestRate);
        this.overdraft = overdraft;
    }

    public double getOverdraft() {
        return overdraft;
    }

    public void setOverdraft(double overdraft) {
        this.overdraft = overdraft;
    }

    //在CheckAccount类中重写withdraw方法，其算法如下：
    //如果（取款金额<账户余额），
    //可直接取款
    //如果（取款金额>账户余额），
    //计算需要透支的额度
    //判断可透支额overdraft是否足够支付本次透支需要，如果可以
    //将账户余额修改为0，冲减可透支金额
    //如果不可以
    //提示用户超过可透支额的限额

    @Override
    public void withdraw (double amount){
        if(balance >=amount){
            super.withdraw(amount);
        }else {
            double needed = amount - balance;
            if(needed <=overdraft){
                super.withdraw(balance);
                overdraft -= needed;
            }else {
                System.out.println("超过可透支限额!");
            }
        }
    }
}
